
<#
.SYNOPSIS
Chocolatey Wrapper

.DESCRIPTION
Wrapper arround the Chocolatey (Chocolatey.org) package wrapper system.

.NOTES

This script will read in a list of chocolatey packages from the MDT Chocolatey variable 
and install each package locally.

.LINK

Microsoft Deployment Toolkit Extensions http://MDTEx.codeplex.com
Copyright Keith Garner, all rights reserved.

http://Chocolatey.org

#>

function Install-ChocolateyPackage 
{
    param(
        [parameter(ValueFromRemainingArguments=$true)]
        [string[]]$Packages
    )

    write-verbose "Construct a local path for Chocolatey"
    if ($env:ChocolateyInstall -eq $Null)
    {
        $env:ChocolateyInstall = [Environment]::GetEnvironmentVariable( "ChocolateyInstall" , [System.EnvironmentVariableTarget]::User)
        if ($env:ChocolateyInstall -eq $Null)
        {
            $env:ChocolateyInstall = join-path ([System.Environment]::GetFolderPath("CommonApplicationData")) "Chocolatey"
        }
    }

    $ChocoExe = join-path $env:ChocolateyInstall "bin\choco.exe"

    write-verbose "Chocolatey Program: $ChocoExe"
    if ( ! (test-path $ChocoExe ) )
    {
        write-verbose "Install Chocolatey..."
        Invoke-Expression ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
        if (!(test-path $ChocoExe))
        {
            throw "Chocolatey Install not found!"
        }
    }

    write-verbose "Install Chocolatey packages from within MDT"
    foreach ( $Package in $Packages ) 
    {
        write-verbose "Install Chocolatey Package: $ChocoExe $Package"
        invoke-expression "cmd.exe /c $chocoExe Install $Package  -y -v 2>&1" | out-verbose
    }

    write-verbose "Chocolatey install done"
}
